package homework.work;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorGUI extends JFrame{
        private JTextField displayField;
        private double num1 = 0;
        private double num2 = 0;
        private char operator = ' ';

        public CalculatorGUI() {
            setTitle("简单计算器");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300, 400);
            setLayout(new BorderLayout());

            displayField = new JTextField();
            displayField.setEditable(false);
            add(displayField, BorderLayout.NORTH);

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(5, 4));

            String[] buttons = {
                    "7", "8", "9", "/",
                    "4", "5", "6", "*",
                    "1", "2", "3", "-",
                    "0", ".", "=", "+",
                    "C"
            };

            for (String buttonText : buttons) {
                JButton button = new JButton(buttonText);
                button.addActionListener(new ButtonClickListener());
                buttonPanel.add(button);
            }

            add(buttonPanel, BorderLayout.CENTER);
            setVisible(true);
        }

        private class ButtonClickListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                String command = e.getActionCommand();
                if (command.matches("[0-9.]")) {
                    displayField.setText(displayField.getText() + command);
                } else if (command.matches("[+\\-*/]")) {
                    num1 = Double.parseDouble(displayField.getText());
                    displayField.setText("");
                    operator = command.charAt(0);
                } else if (command.equals("=")) {
                    num2 = Double.parseDouble(displayField.getText());
                    double result = performCalculation();
                    displayField.setText(Double.toString(result));
                } else if (command.equals("C")) {
                    displayField.setText("");
                    num1 = 0;
                    num2 = 0;
                    operator = ' ';
                }
            }
        }

        private double performCalculation() {
            switch (operator) {
                case '+':
                    return num1 + num2;
                case '-':
                    return num1 - num2;
                case '*':
                    return num1 * num2;
                case '/':
                    if (num2 == 0) {
                        JOptionPane.showMessageDialog(CalculatorGUI.this, "除数不能为零");
                        return 0;
                    }
                    return num1 / num2;
                default:
                    return 0;
            }
        }

        public static void main(String[] args) {
            new CalculatorGUI();
        }
    }

